package me.fung.wubi.app.view.build_table.select

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.material.Button
import androidx.compose.material.ButtonDefaults
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowDropDown
import androidx.compose.material.icons.filled.ExitToApp
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import me.fung.keen.app.view.View
import me.fung.wubi.app.component.imageVectorButton
import me.fung.wubi.app.component.mini

class SelectFileDialogView(
    private val current: SelectFileModel,
    private val onSelected: (SelectFileModel) -> Unit,
    private val onClose: () -> Unit,
) : View<SelectFileDialogPresenter>() {
    @Composable
    override fun content(modifier: Modifier) {
        LaunchedEffect(Unit) {
                p.data = SelectFileModel(current.file)
        }
        Box(
            modifier = Modifier.fillMaxSize().background(Color.LightGray.copy(alpha = 0.9f)).padding(50.dp),
            contentAlignment = Alignment.Center,
        ) {
            Column(
                modifier = Modifier.background(MaterialTheme.colors.background)
                    .padding(horizontal = 30.dp, vertical = 50.dp),
                horizontalAlignment = Alignment.CenterHorizontally,
            ) {
                Text("选择文件")
                Row {
                    Text("文件名", Modifier)
                }
                Row(
                    Modifier.border(1.dp, Color.LightGray).padding(3.dp),
                    verticalAlignment = Alignment.CenterVertically,
                ) {
                    BasicTextField(
                        value = p.data.file,
                        onValueChange = {
                            p.chooseFile()
                        },
                        modifier = Modifier.weight(1f),
                        singleLine = true
                    )
                    Spacer(Modifier.size(3.dp))
                    imageVectorButton(
                        tooltip = "选择文件",
                        icon = Icons.Default.ArrowDropDown,
                        modifier = Modifier.mini(),
                        onClick = p::chooseFile,
                    )

                }
                Spacer(Modifier.size(10.dp))
                // 导航按钮
                bottom(modifier = Modifier.height(ButtonDefaults.MinHeight))

            }
        }
    }

    @Composable
    private fun bottom(modifier: Modifier) {
        Row(
            modifier = modifier
        ) {
            Button(
                onClick = {
                    onSelected(p.data)
                    onClose()
                },
                modifier = Modifier.fillMaxHeight().weight(1f),
            ) {
                Text("确认")
            }
            Spacer(Modifier.width(10.dp))
            imageVectorButton(
                tooltip = "返回",
                icon = Icons.Default.ExitToApp,
                onClick = {
                    onClose()
                },
                modifier = Modifier.fillMaxHeight(),
            )
        }
    }

}
